package com.ch.light.web.controller;

import com.ch.light.core.Response;
import com.ch.light.core.page.Page;
import com.ch.light.mybatis.ParamFilter;
import com.ch.light.web.aop.annotaion.WebLogger;
import com.ch.light.web.model.AppVersion;
import com.ch.light.web.model.WebLog;
import com.ch.light.web.pojo.AppVersionVo;
import com.ch.light.web.service.AppVersionService;
import com.ch.light.web.service.WebLogService;
import com.ch.utils.CommonUtils;
import com.ch.utils.DateUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;


@Controller
@RequestMapping("appVersion")
public class AppVersionController {

    @Autowired
    private AppVersionService appVersionService;

    @GetMapping("list")
    public String list() {
        return "appVersionList";
    }

    @ResponseBody
    @PostMapping("list")
    public Response list(@RequestBody ParamFilter queryFilter) {
        List<AppVersion> records = appVersionService.getList(queryFilter);
        PageInfo<AppVersion> pageInfo = new PageInfo<>(records);
        if (CommonUtils.isNotEmpty(records)) {
            records.forEach(r -> r.setContent(r.getContent() != null ? r.getContent().replaceAll("\\n", "<br>") : ""));
        }
        Page page = queryFilter.getPage();
        page.setResultCount(Long.valueOf(pageInfo.getTotal()).intValue());
        return new Response(records, page);
    }

    @ResponseBody
    @PostMapping("add")
    @WebLogger("添加应用版本")
    public Response add(@RequestBody AppVersionVo record) {
        checkNotNull(record, "应用版本不能为空");
        Date activeAt = DateUtils.parse(record.getActiveAtStr());
        checkArgument(CommonUtils.isNotEmpty(activeAt), "生效时间不能为空");
        record.setActiveAt(activeAt);
        appVersionService.add(record);
        return new Response("添加成功");
    }


    @ResponseBody
    @PostMapping("edit")
    @WebLogger("编辑应用版本")
    public Response edit(@RequestBody AppVersionVo record) {
        Date activeAt = DateUtils.parse(record.getActiveAtStr());
        checkArgument(CommonUtils.isNotEmpty(activeAt), "生效时间不能为空");
        record.setActiveAt(activeAt);
        appVersionService.update(record);
        return new Response("修改成功");
    }

    @ResponseBody
    @PostMapping("delete")
    @WebLogger("删除应用版本")
    public Response delete(@RequestBody List<String> ids) {
        checkArgument(CommonUtils.isNotEmpty(ids), "用户编号不能为空");
        //dataSourceConfigService.delete(ids);
        return new Response("删除成功");
    }

    @ResponseBody
    @PostMapping("detail")
    @WebLogger("查询应用版本详细")
    public Response detail(@RequestBody String code) {
        AppVersion record = appVersionService.findByCode(code);
        AppVersionVo r = new AppVersionVo();
        BeanUtils.copyProperties(record, r);
        r.setActiveAtStr(DateUtils.format(r.getActiveAt()));
        return new Response(r);
    }
}
